package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsDishDao;
import io.renren.modules.app.entity.BsDishEntity;
import io.renren.modules.app.service.BsDishService;
import io.renren.modules.app.utils.FileUtil;
import io.renren.modules.sys.dao.SysUserDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;



@Service("bsDishService")
@Slf4j
public class BsDishServiceImpl extends ServiceImpl<BsDishDao, BsDishEntity> implements BsDishService {

    @Resource
    private BsDishDao bsDishDao ;

    @Override
    public PageUtils queryPage(Map<String, Object> params, HttpServletRequest request) {

        LambdaQueryWrapper<BsDishEntity> wrapper = new LambdaQueryWrapper<>();

        if (params.get("dishName") != null && !params.get("dishName").equals("")){
            wrapper.like(BsDishEntity::getDishName,params.get("dishName"));
        }
        if (params.get("dishType") != null && !params.get("dishType").equals("")){
            wrapper.eq(BsDishEntity::getDishType,params.get("dishType"));
        }
        if (params.get("dishPriceLow") != null && !params.get("dishPriceLow").equals("")){
            wrapper.ge(BsDishEntity::getDishPrice,params.get("dishPriceLow"));
        }
        if (params.get("dishPriceHigh") != null && !params.get("dishPriceHigh").equals("")){
            wrapper.le(BsDishEntity::getDishPrice,params.get("dishPriceHigh"));
        }
        IPage<BsDishEntity> page = this.page(
                new Query<BsDishEntity>().getPage(params),
                wrapper
        );
        //清洗数据
        for (BsDishEntity record : page.getRecords()) {
            if (StringUtils.isNotBlank(record.getDishPhoto())){
                record.setDishPhoto(record.getDishPhoto()+ "?token=" + request.getHeader("token"));
            }
        }

        return new PageUtils(page);
    }

    @Override
    public void saveDish(BsDishEntity bsDish) {
        try {
            //菜品名字重复性校验
            String dishName = bsDish.getDishName();
            List<BsDishEntity> entities = bsDishDao.selectList(
                    new LambdaQueryWrapper<BsDishEntity>()
                            .eq(BsDishEntity::getDishName, dishName));
            if (entities.size() > 0){
                throw new RRException("菜品名称不能重复");
            }
            bsDishDao.insert(bsDish);
        }catch (Exception e){
            log.info("报错类及所在的方法：{}","BsDishService --> saveDish");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public void updateDish(BsDishEntity bsDish) {
        try {
            //菜品名字重复性校验
            String dishName = bsDish.getDishName();
            List<BsDishEntity> entities = bsDishDao.selectList(
                    new LambdaQueryWrapper<BsDishEntity>()
                            .eq(BsDishEntity::getDishName, dishName)
                            .ne(BsDishEntity::getId,bsDish.getId()));
            if (entities.size() > 0){
                throw new RRException("菜品名称不能重复");
            }
            if (StringUtils.isNotBlank(bsDish.getDishPhoto())){
                String dishPhoto = bsDish.getDishPhoto();
                if (dishPhoto.contains("?")) {
                    String[] split = dishPhoto.split("\\?");
                    bsDish.setDishPhoto(split[0]);
                }
            }
            bsDishDao.updateById(bsDish);
        }catch (Exception e){
            log.info("报错类及所在的方法：{}","BsDishService --> updateDish");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public String dishUpload(MultipartFile file) {
        String url= null;//将传递的图片交给FileUtil处理
        try {
            url = FileUtil.uploadimg(file);
            //执行入库操作


            return url ;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public BsDishEntity queryById(Long id,HttpServletRequest request) {
        BsDishEntity bsDish = bsDishDao.selectById(id);
        bsDish.setDishPhoto(bsDish.getDishPhoto() + "?token=" + request.getHeader("token"));
        return bsDish ;
    }

}
